﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

using CustomControlDatabaseInterface.Model;
using CustomControlDatabaseInterface.Utilities;

namespace CustomControlDatabaseInterface.ViewModel
{
    class FilterCustomersViewModel : WorkspaceViewModel {

        public ObservableCollection<String> TechnologiesList { get; set; }

        private String _selectedTechnolgy;
        public String SelectedTechnology {
            get {
                return this._selectedTechnolgy;
            }
            set {
                this._selectedTechnolgy = value;
                OnPropertyChanged("SelectedTechnology");
                if (value != null) {    
                    OnTechnologySelected(_selectedTechnolgy);                
                    GetControlsFromTechnology();
                    SelectedControl = null; 
                    
                }      
            }
        }

        public ObservableCollection<String> ControlsList { get; set; }

        private string _selectedControl; 
        public String SelectedControl {
            get {
                return this._selectedControl;
            }
            set {
                this._selectedControl = value;
                OnPropertyChanged("SelectedControl");
                if (value != null) {    
                    OnControlSelected(SelectedTechnology, SelectedControl);
                }
            }
        }
        public FilterCustomersViewModel() {
            TechnologiesList = new ObservableCollection<string>();
            ControlsList = new ObservableCollection<string>();
           
            ViewModelUtil.UpdateObservableCollection(TechnologiesList,DatabaseUtil.GetTableNames());
            ViewModelUtil.UpdateObservableCollection(ControlsList, DatabaseUtil.GetTableHeaders(SelectedTechnology));
        }

        public delegate void TechnologySelectedHandler( String technology );

        public event TechnologySelectedHandler TechnologySelected;
  
        public void OnTechnologySelected( String technology ) {
            TechnologySelectedHandler handler = this.TechnologySelected; 
            if (TechnologySelected != null) {
                handler(technology);
            }
        }



        public delegate void ControlSelectedHandler(String technology, String control );

        public event ControlSelectedHandler ControlSelected; 

        public void OnControlSelected(String technology, String control ) {
            ControlSelectedHandler handler = this.ControlSelected; 
            if (ControlSelected != null) {
                handler(technology, control);
            }
        }


        #region Private Helpers

        private void GetControlsFromTechnology() {
            ViewModelUtil.UpdateObservableCollection(ControlsList, DatabaseUtil.GetTableHeaders(SelectedTechnology));
            ControlsList.Remove("Kunde");
        }

        #endregion
    }
}
